package tp2;
import java.util.*;
public class StringC {
	private int com;
	private String valor; 
	private int fin;
	private Estadistica es;
	
	
	StringC(String str,Estadistica esEnt){
		es = esEnt;
		com = 0;
		valor = str;
		fin = str.length();
	}
	StringC(StringC str,Estadistica esEnt){
		es = esEnt;
		com = str.com;
		valor = new String(str.valor);
		es.op+=str.valor.length();
		fin = str.fin;
	}
	
	public int length(){
		return fin-com;
	}
	
	public boolean vacia(){
		return length()==0;
	}
	public StringC substring(int begin){
		com = com+begin;
		return this;
	}
	public StringC substring(int begin,int end){
		com = com+begin;
		fin = com+end;
		return this;
	}
	
	public String toString(){
		return valor.substring(com, fin);
	}
	
	public String stringOriginal(){
		return valor;
	}
	
	public StringC concat(StringC str){
		String thisAux = this.valor.substring(com, fin);
		es.op+=fin-com;
		String strAux = str.valor.substring(str.com, str.fin);
		es.op+=str.fin-str.com;
		this.valor = thisAux.concat(strAux);
		this.com = 0; 
		this.fin = this.valor.length();
		return this;
	}
	
	public String concatAstring(StringC str){
		String thisAux = this.valor.substring(com, fin);
		es.op+=fin-com;
		String strAux = str.valor.substring(str.com, str.fin);
		es.op+=str.fin-str.com;
		this.valor = thisAux.concat(strAux);
		return this.valor;
	}
	public Pair<Boolean,Integer > esPrefijoHasta(StringC prefijo){
		if(valor.charAt(com)!= prefijo.valor.charAt(prefijo.com)) return new Pair<Boolean,Integer>(false,new Integer(0));
		int i = 0;
		Pair<Boolean,Integer> res = new Pair<Boolean,Integer>(true,null);
		for(i = 0;i<prefijo.length()&&i<length() &&(valor.charAt(i+com)== prefijo.valor.charAt(i+prefijo.com));i++){
			//if(valor.charAt(i+com)!= prefijo.valor.charAt(i+prefijo.com)) return new Pair<Boolean,Integer>(true,new Integer(i));
			es.op++;
		}
		if(i!=0) return new Pair<Boolean,Integer>(true,new Integer(i));
		else return new Pair<Boolean,Integer>(false,new Integer(0));
	}
	
	public boolean comparar(int begin, StringC str, int beginStr,int tam){
		boolean ret = false;
		if(valor.regionMatches(begin+com, str.valor, beginStr+str.com, 1)){
		es.op+=tam;
		ret =valor.regionMatches(begin+com, str.valor, beginStr+str.com, tam);
	}
		return ret;
	}
	
	public void resetear(){
		com=0;
		fin = valor.length();
	}
	/*private char[] elem;
	private int com = 0;
	private int fin;
	
	StringC(String str){
		elem = new char[str.length()];
		str.getChars(0, str.length()-1, elem, 0);
		fin = str.length()-1;
	}
	public void fin(){
		if(fin-com>0){
			com++;
		}
	}
	
	public boolean esPrefijo(StringC prefijo){
		if(prefijo.size()> this.size()) return false;
		for(int i = 0;i<prefijo.fin-prefijo.com;i++){
			if(elem[i+com]!= prefijo.elem[i+prefijo.com]) return false;
		}
		return true;
	}
	
	public int size(){
		return fin-com;
	}
	
	public void subStringC(StringC str){
		
	}
	*/
}

